/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package algoritmos;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;

/**
 *
 * @author jean
 */
public class Criptografia {

    //SHA-1
    public static String criptografaSha1(String senha) {
        try {
            MessageDigest criptografar = MessageDigest.getInstance("SHA-1");
            criptografar.digest(senha.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return senha = encoder.encode(criptografar.digest());
        } catch (NoSuchAlgorithmException e1) {
            return "";
        }
    }

    //SHA-256
    public static String criptografaSha256(String senha) {
        try {
            MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
            byte messageDigest[] = algorithm.digest(senha.getBytes("UTF-8"));

            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                hexString.append(String.format("%02X", 0xFF & b));
            }
            senha = hexString.toString();
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            System.out.println(e);
            return "";
        }
        return senha;
    }

    //MD5
    public static String criptografaMd5(String senha) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(senha.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(digest.digest());
        } catch (NoSuchAlgorithmException ns) {
            System.out.println(ns);
            return senha;
        }
    }
}